package org.jtb.quickr;

import java.io.InputStream;
import java.util.Map;

class PhotosSearchRequest extends SignedMethodRequest {
    private String staticUrl;
    private PhotosSearchCriteria photosSearchCriteria;
    
    protected PhotosSearchRequest(String baseUrl, String apiKey, String secret, AuthResponse.Token authToken, PhotosSearchCriteria photosSearchCriteria, String staticUrl) throws QuickrException {
        super(baseUrl, apiKey, secret, authToken, "flickr.photos.search");
        this.photosSearchCriteria = photosSearchCriteria;
        this.staticUrl = staticUrl;
    }
    
    
    protected Map<String,String> getArgs() {
        PhotosSearchCriteria psc = photosSearchCriteria;
        
        if (psc.getUserId() != null) {
            putArg("user_id", psc.getUserId());
        }
        if (psc.getTags() != null && psc.getTags().size() > 0) {
            putArg("tags", join(psc.getTags(), ','));
        }
        if (psc.getTagMode() != null) {
            putArg("tag_mode", psc.getTagMode().toString());
        }
        if (psc.getText() != null) {
            putArg("text", psc.getText());
        }
        if (psc.getMinUploadDate() != null) {
            putArg("min_upload_date", Long.toString(psc.getMinUploadDate().getTime()));
        }
        if (psc.getMaxUploadDate() != null) {
            putArg("max_upload_date", Long.toString(psc.getMaxUploadDate().getTime()));
        }
        if (psc.getMinTakenDate() != null) {
            putArg("min_taken_date", Long.toString(psc.getMinTakenDate().getTime()));
        }
        if (psc.getMaxTakenDate() != null) {
            putArg("max_taken_date", Long.toString(psc.getMaxTakenDate().getTime()));
        }
        if (psc.getLicenses() != null && psc.getLicenses().size() > 0) {
            putArg("license", join(psc.getLicenses(), ','));
        }
        if (psc.getSort() != null) {
            putArg("sort", psc.getSort().toString());
        }
        if (psc.getPrivacyFilter() != null) {
            putArg("privacy_filter", psc.getPrivacyFilter().toString());
        }
        if (psc.getBoundingBox() != null) {
            putArg("bbox", psc.getBoundingBox().toString());
        }
        if (psc.getLocationAccuracy() != null) {
            putArg("accuracy", psc.getLocationAccuracy().toString());
        }
        if (psc.getExtras() != null && psc.getExtras().size() > 0) {
            putArg("extras", join(psc.getExtras(), ','));
        }
        if (psc.getPerPage() != null) {
            putArg("per_page", psc.getPerPage().toString());
        }
        if (psc.getPage() != null) {
            putArg("page", psc.getPage().toString());
        }
        
        return super.getArgs();
    }
    
    public PhotosResponse execute() throws QuickrException {
        FlickrUrl furl = new SignedMethodUrl(getBaseUrl(), getArgs(), getApiKey(), getSecret(), getAuthToken(), getMethod());
        
        Get get = new Get(furl.getUrl());
        InputStream is = get.execute();
        
        PhotosResponse pres = new PhotosResponse(is, staticUrl);
        
        return pres;
    }
}
